package com.cbs.module.friend.sample;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import com.cbs.application.activity.CBSActivity;
import com.cbs.application.activity.CBSSimpleActivityResultHandler;
import com.cbs.log.L;
import com.cbs.module.friend.FriendModule;
import com.cbs.module.friend.ModuleHandler;
import com.cbs.module.friend.entity.FriendRelation;
import com.cbs.module.friend.entity.FriendRequest;
import com.cbs.module.user.UserModule;
import com.cbs.module.user.entity.User;
import com.cbs.module.user.ui.UserModuleUI;
import com.cbs.utils.ui.Toast;
import com.google.gson.Gson;

import java.util.List;

/**
 * Created by broche on 1/11/16.
 */
public class MyActivity extends CBSActivity {
    private static final String TAG = MyActivity.class.getName();
    private Gson gson = new Gson();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        findViewById(R.id.main_login).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UserModuleUI.login(MyActivity.this, new CBSSimpleActivityResultHandler() {
                    @Override
                    public void onSuccess(int i, Intent intent) {
                        Toast.show("login");
                    }

                    @Override
                    public void onCancel(int i, Intent intent) {

                    }
                });
            }
        });

        findViewById(R.id.main_friendrequestlist).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FriendModule.getFriendRequestList(MyActivity.this, new ModuleHandler<List<FriendRequest>>() {
                    @Override
                    public void onSuccess(List<FriendRequest> entity) {
                        L.e(TAG, gson.toJson(entity));
                    }

                    @Override
                    public void onFailure(int code, String message) {

                    }

                    @Override
                    public void onException(Exception exception) {

                    }
                });
            }
        });

        findViewById(R.id.main_friendrequestlistcache).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                L.e(TAG, gson.toJson(FriendModule.getFriendRequestListCache()));
            }
        });

        findViewById(R.id.main_friendrequestadd).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MyActivity.this, InputActivity.class);
                intent.putExtra("id", R.id.main_friendrequestadd);
                startActivityForResult(intent, 0);
            }
        });

        findViewById(R.id.main_friendrequestagree).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MyActivity.this, InputActivity.class);
                intent.putExtra("id", R.id.main_friendrequestagree);
                startActivityForResult(intent, 0);
            }
        });

        findViewById(R.id.main_friendrequestreject).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MyActivity.this, InputActivity.class);
                intent.putExtra("id", R.id.main_friendrequestreject);
                startActivityForResult(intent, 0);
            }
        });

        findViewById(R.id.main_friendlist).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FriendModule.getFriendList(MyActivity.this, new ModuleHandler<List<FriendRelation>>() {
                    @Override
                    public void onSuccess(List<FriendRelation> entity) {
                        L.e(TAG, gson.toJson(entity));
                    }

                    @Override
                    public void onFailure(int code, String message) {

                    }

                    @Override
                    public void onException(Exception exception) {

                    }
                });
            }
        });

        findViewById(R.id.main_friendlistcache).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                L.e(TAG, gson.toJson(FriendModule.getFriendListCache()));
            }
        });

        findViewById(R.id.main_frienddelete).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MyActivity.this, InputActivity.class);
                intent.putExtra("id", R.id.main_frienddelete);
                startActivityForResult(intent, 0);
            }
        });

        findViewById(R.id.main_blacklist).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FriendModule.getBlackList(MyActivity.this, new ModuleHandler<List<FriendRelation>>() {
                    @Override
                    public void onSuccess(List<FriendRelation> entity) {
                        L.e(TAG, gson.toJson(entity));
                    }

                    @Override
                    public void onFailure(int code, String message) {

                    }

                    @Override
                    public void onException(Exception exception) {

                    }
                });
            }
        });

        findViewById(R.id.main_blacklistcache).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                L.e(TAG, gson.toJson(FriendModule.getBlackListCache()));
            }
        });

        findViewById(R.id.main_blackadd).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MyActivity.this, InputActivity.class);
                intent.putExtra("id", R.id.main_blackadd);
                startActivityForResult(intent, 0);
            }
        });

        findViewById(R.id.main_blackdelete).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MyActivity.this, InputActivity.class);
                intent.putExtra("id", R.id.main_blackdelete);
                startActivityForResult(intent, 0);
            }
        });

        findViewById(R.id.main_search).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MyActivity.this, InputActivity.class);
                intent.putExtra("id", R.id.main_search);
                startActivityForResult(intent, 0);
            }
        });

        UserModule.connect(new com.cbs.module.user.ModuleHandler<User>() {
            @Override
            public void onSuccess(User entity) {
                L.e(TAG, entity.toString());
            }

            @Override
            public void onFailure(int code, String message) {

            }

            @Override
            public void onException(Exception exception) {

            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode != 0) {
                return;
            }
            String input = data.getStringExtra("input");
            int id = data.getIntExtra("id", -1);
            if (id == R.id.main_friendrequestadd) {
                FriendModule.friendRequestAdd(this, Integer.parseInt(input), new ModuleHandler<FriendRequest>() {
                    @Override
                    public void onSuccess(FriendRequest entity) {
                        L.e(TAG, gson.toJson(entity));
                    }

                    @Override
                    public void onFailure(int code, String message) {

                    }

                    @Override
                    public void onException(Exception exception) {

                    }
                });
            } else if (id == R.id.main_friendrequestagree) {
                FriendModule.friendRequestAgree(this, Integer.parseInt(input), new ModuleHandler() {
                    @Override
                    public void onSuccess(Object entity) {
                        L.e(TAG, gson.toJson(entity));
                    }

                    @Override
                    public void onFailure(int code, String message) {

                    }

                    @Override
                    public void onException(Exception exception) {

                    }
                });
            } else if (id == R.id.main_friendrequestreject) {
                FriendModule.friendRequestReject(this, Integer.parseInt(input), new ModuleHandler() {
                    @Override
                    public void onSuccess(Object entity) {
                        L.e(TAG, gson.toJson(entity));
                    }

                    @Override
                    public void onFailure(int code, String message) {

                    }

                    @Override
                    public void onException(Exception exception) {

                    }
                });
            } else if (id == R.id.main_frienddelete) {
                FriendModule.friendDelete(this, Integer.parseInt(input), new ModuleHandler() {
                    @Override
                    public void onSuccess(Object entity) {
                        L.e(TAG, gson.toJson(entity));
                    }

                    @Override
                    public void onFailure(int code, String message) {

                    }

                    @Override
                    public void onException(Exception exception) {

                    }
                });
            } else if (id == R.id.main_blackadd) {
                FriendModule.BlackAdd(this, Integer.parseInt(input), new ModuleHandler<FriendRelation>() {
                    @Override
                    public void onSuccess(FriendRelation entity) {
                        L.e(TAG, gson.toJson(entity));
                    }

                    @Override
                    public void onFailure(int code, String message) {

                    }

                    @Override
                    public void onException(Exception exception) {

                    }
                });
            } else if (id == R.id.main_blackdelete) {
                FriendModule.blackDelete(this, Integer.parseInt(input), new ModuleHandler() {
                    @Override
                    public void onSuccess(Object entity) {
                        L.e(TAG, gson.toJson(entity));
                    }

                    @Override
                    public void onFailure(int code, String message) {

                    }

                    @Override
                    public void onException(Exception exception) {

                    }
                });
            } else if (id == R.id.main_search) {
                List<FriendRelation> res = FriendModule.getFriendListCache(input);
                L.e(TAG, gson.toJson(res));
            }
        }
    }
}
